//
//  HomeViewController.m
//  NewsDemo
//
//  Created by 何欣 on 15/7/2.
//  Copyright (c) 2015年 何欣. All rights reserved.
//

#import "HomeViewController.h"

#define HomeScrollHeight 40

@interface HomeViewController ()<ScrollHeaderDelegate,UICollectionViewDataSource,UICollectionViewDelegate,HomeCollectionViewCellDelegate>

@property(nonatomic,retain)NSMutableArray *topicArr;//话题数组

@property(nonatomic,retain)UICollectionView *collectionView;

@property(nonatomic,retain)HomeScrollHeader *scrollHeader;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"新闻";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.topicArr = [NSMutableArray array];
    [self getTopicData];
}
#pragma mark- 获取数据

#pragma mark 获取话题数据
/**
 *  从本地文件中读取 “话题” 的数据
 */
-(void)getTopicData{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"topic_news" ofType:@"json"];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *fileDictonary = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:nil];
    for (NSDictionary *dic in [fileDictonary objectForKey:@"tList"]) {
        HomeTopicModel *topicModel = [HomeTopicModel homeTopicModelWithDictonary:dic];
        [self.topicArr addObject:topicModel];
    }
    
    //根据获取的数据创建头部滚动视图
    [self creatScrollHeader:_topicArr];
    [self creatCollectionView];

}


#pragma mark- 创建视图

#pragma mark 创建头部滚动视图
-(void)creatScrollHeader:(NSMutableArray *)topicArr{
    _scrollHeader = [[HomeScrollHeader alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, HomeScrollHeight)];
    _scrollHeader.delegate = self;
    [_scrollHeader setScrollHeaderButtonTitleArray:topicArr];
    [self.view addSubview:_scrollHeader];
}

#pragma mark 创建整体滚动视图 collectionView
-(void)creatCollectionView{

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight - HomeScrollHeight - 64);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + HomeScrollHeight ,KScreenWidth, KScreenHeight - HomeScrollHeight - 64) collectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
}
#pragma mark- delegate

#pragma mark ScrollHeaderDelegate
/**
 *  点击按钮的响应方法
 *
 *  @param index 按钮的tag值,根据其计算collectionView滚动到得item
 */
-(void)clickButtonAtIndex:(NSInteger)index{

    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0 ] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}


#pragma mark  HomeCollectionViewCellDelegate

-(void)homeCollectionViewCellDidSelectTableViewCell:(NSString *)webViewRequestUrl{
    HomeNewsDetailController *detailContoller = [[HomeNewsDetailController alloc]init];
    detailContoller.webViewRequestUrl = webViewRequestUrl;
    [self.navigationController pushViewController:detailContoller animated:YES];
}


#pragma mark  CollectionDelgate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _topicArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
    cell.tid = [_topicArr[indexPath.row] tid];
    //设置HomeCollectionViewCellDelegate 代理
    cell.delegate = self;
    NSLog(@"%@",cell.tid);
    
    return cell;
    
}

#pragma mark scrollViewDelegate

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [_scrollHeader setScrollToIndex:scrollView.contentOffset.x / KScreenWidth ];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
